package com.zxl.live.screen.screen.ui.helper;

import android.text.TextUtils;

import com.zxl.live.screen.screen.constant.LockerEnv;
import com.zxl.live.screen.screen.ui.helper.data.SuiScreenDataHelper;
import com.zxl.live.tools.configs.AdsConfigsHelper;
import com.zxl.live.tools.tools.FileUtil;
import com.zxl.live.tools.tools.HttpUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/1/24.
 */

public class ServiceDataHelper implements Callback {

    public static final String MIN = "min";

    public static final String PREVIEW = "preview";

    public static final String MAX = "max";

    private Map<String, List<String>> mAnimCache;

    private Map<String, List<String>> mSystemErrorCache;

    private Map<String, String> mSuiCache;

    private Map<String, String> mKongBuCache;

    private static ServiceDataHelper sInstance;

    private String path;

    public static ServiceDataHelper getInstance() {
        if (null == sInstance) {
            sInstance = new ServiceDataHelper();
        }
        return sInstance;
    }

    private ServiceDataHelper() {
        mAnimCache = new HashMap<>();
        mSystemErrorCache = new HashMap<>();
        mSuiCache = new HashMap<>();
        mKongBuCache = new HashMap<>();
        String url = AdsConfigsHelper.getConfigParams("screen_info");
        if (!TextUtils.isEmpty(url)) {
            path = LockerEnv.Path.ICON_PATH + url.hashCode();
            if (FileUtil.isFileExist(path)) {
                parse();
            } else {
                HttpUtils.doGet(url, this);
            }
        }
    }

    @Override
    public void onFailure(Call call, IOException e) {
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        FileUtil.saveByteToSDFile(response.body().bytes(), path);
        parse();
    }

    public void parse() {
        try {
            JSONObject jsonObject = new JSONObject(FileUtil.readFileToString(path));
            JSONObject animJsonObject = jsonObject.optJSONObject("anim");
            mAnimCache.put(MIN, parseJsonArray(animJsonObject.optJSONArray(MIN)));
            mAnimCache.put(PREVIEW, parseJsonArray(animJsonObject.optJSONArray(PREVIEW)));
            mAnimCache.put(MAX, parseJsonArray(animJsonObject.optJSONArray(MAX)));

            JSONObject systemErrorJsonObject = jsonObject.optJSONObject("system_error");
            mSystemErrorCache.put(MIN, parseJsonArray(systemErrorJsonObject.optJSONArray(MIN)));
            mSystemErrorCache.put(PREVIEW, parseJsonArray(systemErrorJsonObject.optJSONArray(PREVIEW)));
            mSystemErrorCache.put(MAX, parseJsonArray(systemErrorJsonObject.optJSONArray(MAX)));

            JSONObject suiJsonObject = jsonObject.optJSONObject("sui");
            mSuiCache.put(MIN, suiJsonObject.optString(MIN));
            mSuiCache.put(PREVIEW, suiJsonObject.optString(PREVIEW));
            mSuiCache.put(MAX, suiJsonObject.optString(MAX));

            JSONObject kongBuJsonObject = jsonObject.optJSONObject("kongbu");
            mKongBuCache.put(MIN, kongBuJsonObject.optString(MIN));
            mKongBuCache.put(PREVIEW, kongBuJsonObject.optString(PREVIEW));
            mKongBuCache.put(MAX, kongBuJsonObject.optString(MAX));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<String> parseJsonArray(JSONArray jsonArray) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            list.add(jsonArray.optString(i));
        }
        return list;
    }

    public String getUrl(SuiScreenDataHelper.DataType dataType, String size, int index) {
        try {
            if (dataType == SuiScreenDataHelper.DataType.ANIMATION) {
                return mAnimCache.get(size).get(index);
            } else if (dataType == SuiScreenDataHelper.DataType.KUONGBU) {
                return String.format(mKongBuCache.get(size), index + 1);
            } else if (dataType == SuiScreenDataHelper.DataType.SUI) {
                return String.format(mSuiCache.get(size), index + 1);
            } else if (dataType == SuiScreenDataHelper.DataType.SYSTEM_ERROR) {
                return mSystemErrorCache.get(size).get(index);
            }
        } catch (Exception e) {
        }
        return null;
    }



}
